#!/usr/bin/env python

import os, sys, subprocess
import unittest

import ConfigParser, json

SITE_ROOT = os.path.dirname(os.path.realpath(__file__))
path = os.path.normpath(SITE_ROOT + '/../')
if path not in sys.path:
	sys.path.append(path)

DMQ_EXE = "%s/dmq/dmq" % (path)
DMQ_INI = "%s/test/dmq.ini" % (path)

class ZMQClient(object):

	"""docstring for ZMQClient"""
	def __init__(self, dmq_ini):
		self.dmq_ini = dmq_ini

		cfg = ConfigParser.SafeConfigParser()
		try:
			# Make sure we can access the config file
			if not os.access( self.dmq_ini, os.F_OK | os.R_OK ):
				raise( "Unable to access/open config file at:%s" % (self.dmq_ini) )

			cfg.read(self.dmq_ini)
		except Exception, e:
			print( "Exception: %s" % e )
			usage()

		self.zmq_host = cfg.get( cfg_section, 'zmq_host' )
		self.zmq_port = cfg.getint( cfg_section, 'zmq_port' )
		self.zmq_transport = cfg.get( cfg_section, 'zmq_transport' )
		self.zmq_debug = cfg.getboolean( cfg_section, 'zmq_debug' )

		self.context = zmq.Context()
		self.socket = self.context.socket(zmq.REQ)

		self.socket.bind("%s://%s:%d" % (
				self.zmq_transport, self.zmq_host, self.zmq_port))

	def send(self, url, params={}, method='GET'):
		"""
		"""

		req = {'url':url,
			'method':'GET',
		 	'params':params,
		 }

		self.socket.send(json.dumps(req))
		return self.socket.recv()
	#send()
		
#ZMQClient

class TestEcho(unittest.TestCase):

	def setUp(self):
		"""
		Start the dmq service
		"""

		global path
		global DMQ_EXE
		global DMQ_INI

		print("DMQ_EXE : %s" % (DMQ_EXE,))
		print("DMQ_INI : %s" % (DMQ_INI,))

		proc_args = [DMQ_EXE, '-f', DMQ_INI, 'start']
		print("Start the service!")
		print(" ".join(proc_args))

		env = os.environ
		env['PYTHONPATH'] = "%s:$PYTHONPATH" % (path,)
		subprocess.check_call(proc_args)
	#setUp()

	def tearDown(self):
		"""docstring for tearDown"""

		global DMQ_EXE
		global DMQ_INI

		proc_args = [DMQ_EXE, '-f', DMQ_INI, 'stop']
		print("Stop the service!")
		print(" ".join(proc_args))

		env = os.environ
		env['PYTHONPATH'] = "%s:$PYTHONPATH" % (path,)
		subprocess.check_call(proc_args)
	#tearDown()

	def test_echo(self):
		"""docstring for test_echo"""
		print("Test echo")
	#test_echo()
#class

	
if __name__ == '__main__':
	unittest.main()
